package org.example.myleet.p643;

public class Solution {
    /**
     * 4 ms
     */
    public double findMaxAverage(int[] nums, int k) {
        long sum = 0;
        for (int i=0; i<k; i++) {
            sum += nums[i];
        }
        double maxAvg = Integer.MIN_VALUE;
        for (int i=0; i<nums.length-k; i++) {
            maxAvg = Math.max(maxAvg, (double) sum / k);
            //滑窗计算出新的和
            sum += (nums[i+k] - nums[i]);
        }
        return Math.max(maxAvg, (double) sum / k);
    }
}
